{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 278,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import math\n",
    "\n",
    "data = pd.read_csv(\"../../data/clean_weather.csv\", index_col=0)\n",
    "data = data.ffill()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 279,
   "outputs": [],
   "source": [
    "PREDICTORS = [\"tmax\", \"tmin\", \"rain\"]\n",
    "TARGET = \"tmax_tomorrow\"\n",
    "\n",
    "np.random.seed(0)\n",
    "split_data = np.split(data, [int(.7 * len(data)), int(.85 * len(data))])\n",
    "(train_x, train_y), (valid_x, valid_y), (test_x, test_y) = [[d[PREDICTORS].to_numpy(), d[[TARGET]].to_numpy()] for d in\n",
    "                                                            split_data]"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 280,
   "outputs": [],
   "source": [
    "def mse(actual, predicted):\n",
    "    return np.mean((actual - predicted) ** 2)\n",
    "\n",
    "\n",
    "def mse_grad(actual, predicted):\n",
    "    return (predicted - actual)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 281,
   "outputs": [],
   "source": [
    "def init_params(predictors, targets):\n",
    "    k = math.sqrt(1 / predictors)\n",
    "    np.random.seed(0)\n",
    "    weights = np.random.rand(predictors, targets) * 2 * k - k\n",
    "    biases = np.ones((1, targets)) * 2 * k - k\n",
    "    return  [weights, biases]\n",
    "\n",
    "def forward(params, x):\n",
    "    weights, biases = params\n",
    "    prediction = x @ weights + biases\n",
    "    return prediction\n",
    "\n",
    "def backward(params, x, lr, grad):\n",
    "    w_grad = (x.T / x.shape[0]) @ grad\n",
    "    b_grad = np.mean(grad, axis=0)\n",
    "\n",
    "    params[0] -= w_grad * lr\n",
    "    params[1] -= b_grad * lr\n",
    "\n",
    "    return params"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 285,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 0 validation loss: 297.27540770706065\n",
      "Epoch 10000 validation loss: 22.647249675483117\n",
      "Epoch 20000 validation loss: 22.613777406314743\n",
      "Epoch 30000 validation loss: 22.581085439005864\n",
      "Epoch 40000 validation loss: 22.549154158580198\n"
     ]
    }
   ],
   "source": [
    "lr = 1e-4\n",
    "epochs = 50000\n",
    "params = init_params(train_x.shape[1], train_y.shape[1])\n",
    "\n",
    "for i in range(epochs):\n",
    "    predictions = forward(params, train_x)\n",
    "    grad = mse_grad(train_y, predictions)\n",
    "\n",
    "    params = backward(params, train_x, lr, grad)\n",
    "\n",
    "    if i % 10000 == 0:\n",
    "        predictions = forward(params, valid_x)\n",
    "        valid_loss = mse(valid_y, predictions)\n",
    "\n",
    "        print(f\"Epoch {i} validation loss: {valid_loss}\")"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "params"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "predictions = forward(params, test_x)\n",
    "mse(test_y, predictions)"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
